package com.wl.user.login.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wl.entity.Lvuser;
import com.wl.user.login.biz.ILvuserBiz;
import com.wl.user.login.mapper.LvuserMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import java.time.LocalDate;
import java.time.Period;
import java.util.regex.Pattern;

@Service
public class LvuserBizImpl extends ServiceImpl<LvuserMapper, Lvuser> implements ILvuserBiz {
    private static final Pattern ZHANGHAO_PATTERN = Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5]+$");
    private static final Pattern EMAIL_PATTERN = Pattern.compile("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
    private static final int MIN_PASSWORD_LENGTH = 6;

    @Autowired
    private LvuserMapper lvuserMapper;

    @Override
    public void register(String head, String uname, String zhanghao, String password, String email) {
        // 先检查账号和邮箱是否已存在
        if (lvuserMapper.countByZhanghao(zhanghao) > 0) {
            throw new RuntimeException("账号已存在，请重新输入");
        }
        if (lvuserMapper.countByEmail(email) > 0) {
            throw new RuntimeException("邮箱已注册，请重新输入");
        }

        // 获取当前时间作为创建时间和更新时间
        Date now = new Date();
        lvuserMapper.insert(head, uname, zhanghao, password, email, now, now);
    }

    @Override
    public boolean login(String head,String zhanghao, String password) {
        Lvuser user = findByZhanghao(zhanghao);
        return user != null && user.getPassword().equals(password);

    }

    @Override
    public Lvuser findByZhanghao(String zhanghao) {
        return lvuserMapper.findByZhanghao(zhanghao);
    }

    @Override
    public Lvuser findByEmail(String email) {
        return lvuserMapper.findByEmail(email);
    }

    @Override
    public List<Lvuser> selectByZhanghao(String zhanghao) {
        return lvuserMapper.selectByZhanghao(zhanghao);
    }

    //修改基本信息
    @Override
    public int updateBasicInfo(String zhanghao, String uname, Integer gender, String birthday, String bio) {
        // 参数校验 只有当参数不为空时才效验
        if (uname != null) {
            if (uname.trim().isEmpty()) {
                throw new IllegalArgumentException("用户名不能为空");
            }
        }
        if (gender != null && (gender < 0 || gender > 2)) {
            throw new IllegalArgumentException("性别参数不合法");
        }
        if (birthday != null && !birthday.matches("^\\d{4}-\\d{2}-\\d{2}$")) {
            throw new IllegalArgumentException("生日格式应为YYYY-MM-DD");
        }

        return lvuserMapper.updateBasicInfo(zhanghao, uname, gender, birthday, bio);
    }

    //修改头像
    @Override
    public int updateHead(String zhanghao, String head) {
        if (head == null || !head.startsWith("img/") || !head.endsWith(".png")) {
            throw new IllegalArgumentException("头像路径不合法");
        }
        return lvuserMapper.updateHead(zhanghao, head);
    }

    //修改邮箱
    @Override
    public int updateEmail(String zhanghao, String email) {
//        if (!isValidEmail(email)) {
//            throw new IllegalArgumentException("邮箱格式不正确");
//        }
        if (lvuserMapper.countByEmail(email) > 0) {
            throw new RuntimeException("邮箱已被其他账号使用");
        }
        return lvuserMapper.updateEmail(zhanghao, email);
    }

    //修改账号密码
    @Override
    public int updatePassword(String zhanghao, String newPassword) {
        if (newPassword == null || newPassword.length() < MIN_PASSWORD_LENGTH) {
            throw new IllegalArgumentException("密码长度不能少于" + MIN_PASSWORD_LENGTH + "位");
        }
        return lvuserMapper.updatePassword(zhanghao, newPassword);
    }

    @Override
    public int deleteZhanghao(String zhanghao, String password) {
        // 参数校验
        if(StringUtils.isEmpty(zhanghao)){
            throw new IllegalArgumentException("账号不能为空");
        }
        if(StringUtils.isEmpty(password)){
            throw new IllegalArgumentException("密码不能为空");
        }

        // 查询用户
        Lvuser user = lvuserMapper.findOneByZhanghao(zhanghao); // ✅ 正确
        if (user == null) {
            throw new IllegalArgumentException("账号不存在");
        }

        // 密码验证
        if (!password.equals(user.getPassword())) {
            throw new SecurityException("密码错误，请重新输入");
        }

        // 执行删除
        return lvuserMapper.deleteZhanghao(zhanghao);
    }

    @Override
    public List<Lvuser> getBatchUserInfo(List<Integer> uids) {
        if (uids == null || uids.isEmpty()) {
            return Collections.emptyList();
        }

        //使用MyBatis-Plus的in查询
        LambdaQueryWrapper<Lvuser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Lvuser::getUid, uids);
        return this.list(queryWrapper);
    }
}